package com.emcom.gotour.service;

import java.util.List;

import com.emcom.gotour.dao.TourContentDao;
import com.emcom.gotour.dao.TourSchedulerDao;
import com.emcom.gotour.enums.BooleanEnum;
import com.emcom.gotour.enums.TourType;
import com.emcom.gotour.po.TourContent;
import com.emcom.gotour.po.TourScheduler;
import com.emcom.gotour.vo.TourContentVO;

public class TourContentServiceImpl implements TourContentService {

	private TourContentDao tourContentDao;

	private TourSchedulerDao tourSchedulerDao;

	private int showCount = 4;

	private int categoryCount = 10;

	private int popularCount = 20;

	public TourContent queryAd() {
		TourContent parameters = new TourContent();
		parameters.setIsAd(BooleanEnum.TRUE.name());
		List<TourContent> tourContentList = tourContentDao.query(parameters);
		if(tourContentList.size() > 0) {
			return tourContentList.get(0);
		}
		// default ad.<br/>
		TourContent defaultAd = new TourContent();
		defaultAd.setId(Long.valueOf(1));
		defaultAd.setAdImageUrl("images/default_ad.jpg");
		return defaultAd;
	}

	public List<String> queryCategory(TourType tourType) {
		return tourContentDao.queryCategory(tourType);
	}

	public List<TourContent> queryCategoryContent(String category) {
		TourContent parameters = new TourContent();
		parameters.setCategory(category);
		List<TourContent> tourContentList = tourContentDao.query(parameters);
		int size = tourContentList.size();
		if(size >= categoryCount) {
			return tourContentList.subList(0, categoryCount);
		}
		return tourContentList;
	}

	public List<TourContent> queryAllCategoryContent(String category) {
		TourContent parameters = new TourContent();
		parameters.setCategory(category);
		return tourContentDao.query(parameters);
	}

	public TourContentVO queryContent(long id) {
		TourContent parameters = new TourContent();
		parameters.setId(id);
		List<TourContent> tourContentList = tourContentDao.query(parameters);
		if(tourContentList.size() == 1) {
			TourContent tourContent = tourContentList.get(0);
			TourScheduler tourScheduler = new TourScheduler();
			tourScheduler.setTourId(tourContent.getId());
			List<TourScheduler> tourSchedulerList = tourSchedulerDao.query(tourScheduler);
			// -- TourContentVO Bean.<br/>
			TourContentVO tourContentVO = new TourContentVO();
			tourContentVO.setId(tourContent.getId());
			tourContentVO.setTypeId(tourContent.getTypeId());
			tourContentVO.setTypeName(TourType.valueOf(tourContent.getTypeId()).value());
			tourContentVO.setCategory(tourContent.getCategory());
			tourContentVO.setSubject(String.valueOf(tourContent.getSubject()));
			tourContentVO.setAim(String.valueOf(tourContent.getAim()));
			tourContentVO.setCharacteristic(String.valueOf(tourContent.getCharacteristic()));
			tourContentVO.setLocation(String.valueOf(tourContent.getLocation()));
			tourContentVO.setDays(tourContent.getDays());
			tourContentVO.setCost(tourContent.getCost());
			tourContentVO.setCostStandard(tourContent.getCostStandard());
			tourContentVO.setCostNotice(tourContent.getCostNotice());
			tourContentVO.setAttention(tourContent.getAttention());
			tourContentVO.setStatement(tourContent.getStatement());
			tourContentVO.setComments(String.valueOf(tourContent.getComments()));
			tourContentVO.setIsAd(tourContent.getIsAd());
			tourContentVO.setAdImageUrl(String.valueOf(tourContent.getAdImageUrl()));
			tourContentVO.setIsImage(tourContent.getIsImage());
			tourContentVO.setImageUrl(String.valueOf(tourContent.getImageUrl()));
			tourContentVO.setImageUrl1(String.valueOf(tourContent.getImageUrl1()));
			tourContentVO.setImageUrl2(String.valueOf(tourContent.getImageUrl2()));
			tourContentVO.setImageUrl3(String.valueOf(tourContent.getImageUrl3()));
			tourContentVO.setImageUrl4(String.valueOf(tourContent.getImageUrl4()));
			tourContentVO.setInputDate(tourContent.getInputDate());
			tourContentVO.setUpdateDate(tourContent.getUpdateDate());
			tourContentVO.setTourSchedulerList(tourSchedulerList);
			return tourContentVO;
		}
		return null;
	}

	public List<TourContent> queryImageContent(TourType tourType) {
		TourContent parameters = new TourContent();
		parameters.setTypeId(tourType.name());
		parameters.setIsImage(BooleanEnum.TRUE.value());
		List<TourContent> tourContentList = tourContentDao.query(parameters);
		int size = tourContentList.size();
		if(size >= showCount) {
			return tourContentList.subList(0, showCount);
		}
		return tourContentList;
	}

	public List<TourContent> queryTypeContent(TourType tourType) {
		TourContent parameters = new TourContent();
		parameters.setTypeId(tourType.name());
		return tourContentDao.query(parameters);
	}

	public List<TourContent> queryPopularContent() {
		TourContent parameters = new TourContent();
		List<TourContent> tourContentList = tourContentDao.query(parameters);
		int size = tourContentList.size();
		if(size >= popularCount) {
			return tourContentList.subList(0, popularCount);
		}
		return tourContentList;
	}

	public void setTourContentDao(TourContentDao tourContentDao) {
		this.tourContentDao = tourContentDao;
	}

	public void setTourSchedulerDao(TourSchedulerDao tourSchedulerDao) {
		this.tourSchedulerDao = tourSchedulerDao;
	}

	public void setShowCount(int showCount) {
		this.showCount = showCount;
	}

	public void setCategoryCount(int categoryCount) {
		this.categoryCount = categoryCount;
	}

	public void setPopularCount(int popularCount) {
		this.popularCount = popularCount;
	}

}
